// 保持1111
import axios from 'axios';

//通过id获取用户
export const getUserById = async (id) => {
    try {
        const response = await axios.get(`http://localhost:8080/Users/${id}`);
        return response.data;
    } catch (error) {
        console.error('Error getting user:', error.response ? error.response.data : error.message);
        throw error;
    }
};

// 获取用户信息函数
export const getUserByUsername = async (username) => {
    try {
        const response = await axios.get(`http://localhost:8080/Users/getUser/${username}`);
        return response.data;
    } catch (error) {
        console.error('Error getting user:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//获取所有用户列表
export const getUserList = async () => {
    try {
        const response = await axios.get(`http://localhost:8080/Users`);
        return response.data;
    } catch (error) {
        console.error('Error getting user:', error.response ? error.response.data : error.message);
        throw error;
    }
};

// 注册用户函数
export const registerUser = async (userData) => {
    try {
        const response = await axios.post('http://localhost:8080/Users', userData);
        return response.data;
    } catch (error) {
        console.error('Error registering user:', error.response ? error.response.data : error.message);
        throw error;
    }
};

// 更新用户
export const updateUser = async (userData) => {
    try {
        const response = await axios.put(`http://localhost:8080/Users`,userData);
        return response.data;
    } catch (error) {
        console.error('Error getting user:', error.response ? error.response.data : error.message);
        throw error;
    }
};
//删除指定id的用户
export const deleteUser = async (id) => {
    try {
        const response = await axios.delete(`http://localhost:8080/Users/${id}`);
        return response.data;
    } catch (error) {
        console.error('Error getting user:', error.response ? error.response.data : error.message);
        throw error;
    }
};



// 用户登录函数
export const loginUser = async (loginData) => {
    try {
        const response = await axios.post('http://localhost:8080/login', loginData);
        return response.data;
    } catch (error) {
        console.error('Error logging in:', error.response ? error.response.data : error.message);
        throw error;
    }
};




